
use "$data/allmergeprep50_ready.dta", clear
append using "$data/allmergeprep55.dta"
append using "$data/allmergeprep60.dta" 
append using "$data/allmergeprep61.dta"
append using "$data/allmergeprep66.dta"


drop if prin_acty_occ=="X01"| prin_acty_occ=="X02"| prin_acty_occ=="X09"| prin_acty_occ=="X10"| prin_acty_occ=="X99"| prin_acty_occ=="XXX"|prin_acty_occ=="X00"
drop if subs_acty_occ=="X01"| subs_acty_occ=="X02"| subs_acty_occ=="X09"| subs_acty_occ=="X10"| subs_acty_occ=="X99"| subs_acty_occ=="XXX"
destring subs_acty_occ, replace 

*saveold "NSSdata_unemp_emp_survey", replace
*drop if year==200304 
keep if inrange(age,18,65)

*Treated state - Andhra Pradesh
gen treat = (state == 28)


g post = (year==200405)
replace post =1 if year==200304
replace post =1 if year==200910

g posttreat = post*treat
egen stateyear = group(state year)
gen indcode = int(prin_acty_ind/1000)
replace indcode = int(prin_acty_ind/100) if year==199394
gen threedigitind = int(prin_acty_ind/100)
replace threedigitind = int(prin_acty_ind/10) if year==199394

*Working status of the worker
gen wageworker = inrange(prin_acty_stat,31,51)

keep if prin_acty_stat<=51
gen sample_nohh = inrange(prin_acty_stat,21,51)



*Casual workers
gen casualworker  = 0 
replace casualworker = 1 if prin_acty_stat == 11 | prin_acty_stat == 21 ///
							| prin_acty_stat == 41 | prin_acty_stat == 51



*Works in an informal firm
gen formal_firm=0 if !missing(numworkers) & !missing(electricity)
replace formal_firm=1 if  numworkers>=4  | (numworkers>=3 & electricity==1)
recode formal_firm (0=1) (1=0), gen(informalfirm)
replace informalfirm = . if missing(numworkers)
replace informalfirm = 1 if prin_acty_stat == 21 
replace informalfirm = 1 if prin_acty_stat == 11 
replace informalfirm=. if year==199394|year==200304



*Is a formal worker
gen formalworker=0
replace formalworker =1 if mode_pay1==3|mode_pay1==4 | ///
							mode_pay1==13|mode_pay1==14 | ///
							mode_pay1==18|mode_pay1==19 | ///
							mode_pay1==18|mode_pay1==29
replace formalworker = . if missing(mode_pay1)
recode formalworker (0=1) (1=0), gen(informalworker)
replace informalworker=1 if prin_acty_stat == 21
replace informalworker=. if year==199394|year==200304


*Classifying weekly wages as formal as well
gen formalworker2=0
replace formalworker2 =1 if mode_pay1==2|mode_pay1==3|mode_pay1==4 | ///
							mode_pay1==12| mode_pay1==13|mode_pay1==14 | ///
							mode_pay1==17| mode_pay1==18|mode_pay1==19 | ///
							mode_pay1==27|mode_pay1==18|mode_pay1==29
replace formalworker2 = . if missing(mode_pay1)
recode formalworker2 (0=1) (1=0), gen(informalworker2)
replace informalworker2=1 if prin_acty_stat == 21
replace informalworker2=. if year==199394|year==200304


*Daily Wages
gen dwage_wind = dwage 
qui summ dwage, de 
replace dwage_wind = . if dwage>r(p99)
replace dwage_wind = . if inrange(prin_acty_stat,11,21)
gen lndwage = ln(dwage)
gen lndwage_wind = ln(dwage_wind)

*Hourly wages
gen hwage_wind = dwage_wind/8 
gen lnhwage_wind = ln(hwage_wind)

*Generating the variables excluding household work
foreach var of varlist casualworker informalfirm informalworker informalworker2 lndwage_wind {
	gen `var'_nohh = `var'
	replace `var'_nohh = . if sample_nohh == 0
}

*Individual controls
recode  genedu_recode (0 1 2 3 =0) (4 5=1), gen(highedu)

*Any high education is 1 if technical education or college and above general education is obtained 
gen anyhighedu = 0
replace anyhighedu=1 if genedu_recode==5 | tech_edu!=1 

***Unemployed
gen unemployed = (prin_acty_stat==81)

***Agricultural worker
gen agworker = (indcode<10) 
replace agworker =. if indcode==.

gen age2 = age*age

gen married = (marstatus>=2) 
replace married = . if marstatus==.

gen education =0
replace education = 1 if gen_edu==8|gen_edu==7
replace education = 2 if gen_edu>=9

gen techedu_recode =0
replace techedu_recode =1 if  tech_edu!=1

egen Dcode = group(state district)

g fakepost = (year==199900)
g placebo = fakepost*treat

tab year, gen(year)
gen treat9394 = year1 * treat
gen treat9900 = year2 * treat
gen treat0304 = year3 * treat
gen treat0405 = year4 * treat
gen treat0910 = year5 * treat 
gen zero=0

label variable treat9394 "1993-94" 
label variable treat9900 "1999-00"
label variable treat0304 "2003-04"
label variable treat0405 "2004-05"
label variable zero "1999-00"
label variable posttreat "Post X Treat"
label variable placebo "Placebo"

gen occlen = strlen(prin_acty_occ)
gen prin_occ_2digit = prin_acty_occ
replace prin_occ_2digit = substr(prin_acty_occ,1,2) if occlen>2
destring prin_occ_2digit, replace
destring prin_acty_occ, replace


*Set the Fixed Effects, Clusters and Controls
global controls  "age age2 sex i.sgroup married i.gen_lit"
global fe_m "state year state##indcode state#c.year indcode#year"
global fe_main "state year indcode#year"
global cl "state"

gen sample_main = year>=199900 & year<200910

 

*******************************************************
* 1. NSS REGRESSIONS FOR THE MAIN PAPER WITH WEIGHTS
*******************************************************
***Table 4 and Appendix Table A9 in the paper
est clear 
foreach var of varlist informalworker informalfirm casualworker {

	 qui eststo reg_`var': reghdfe `var' ${controls} posttreat /// 
			 [pw=weight] if indcode>=15 & sample_main == 1 ///
		 , cluster($cl) absorb($fe_main)
	    qui estadd local fes "Yes"
    	qui estadd local controls "No"
    	qui estadd scalar rsq = e(r2)
    	qui summ `var' if treat == 0 & post == 0 & indcode>=15 
    	qui estadd scalar mean = r(mean)


	 qui eststo regm_`var': reghdfe `var'_nohh ${controls} posttreat /// 
			 [pw=weight] if indcode>=15  & sample_main == 1  & sample_nohh == 1 ///
		 , cluster($cl) absorb($fe_main)
	    qui estadd local fes "Yes"
    	qui estadd local controls "No"
    	qui estadd scalar rsq = e(r2)
    	qui summ `var' if treat == 0 & post == 0 & indcode>=15 & indcode<=37
    	qui estadd scalar mean = r(mean)


    	noi di "Done with `var'"
}

foreach var of varlist lndwage_wind {

	 qui eststo xreg_`var': reghdfe `var' ${controls} posttreat /// 
			 [pw=weight] if indcode>=15 & sample_main == 1 ///
		 , cluster($cl) absorb($fe_main)
	    qui estadd local fes "Yes"
    	qui estadd local controls "No"
    	qui estadd scalar rsq = e(r2)
    	qui summ `var' if treat == 0 & post == 0 & indcode>=15 
    	qui estadd scalar mean = r(mean)
    	noi di "Done with `var'"
}

**Table 4
# delimit ;
esttab  reg_* xreg* using "${output}/nss/Table4.tex", replace
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Probability of Working In" "Log", pattern(1 0 0 1)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Informal Contract" "Informal Firm" "Casual Work" "Daily Wage")
stats(mean rsq N, labels("Control, 1991 Mean" "R2" "N")
fmt(2 2 0)) collabels(none) label booktabs nonotes
;
#delimit cr	

**Table A9
# delimit ;
esttab  reg_* using "${output}/nss/TableA9.tex", replace
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Probability of Working In" " ", pattern(1 0 0 1)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Informal Contract" "Informal Firm" "Casual Work" "Wage")
stats(mean rsq N, labels("Control, Pre Mean" "R2" "N")fmt(2 2 0)) 
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel A: Including Self-Employed Individuals}}} \\ \midrule")
postfoot("")
collabels(none) label booktabs nonotes
;
#delimit cr	


# delimit ;
esttab  regm_* using "${output}/nss/TableA9.tex", append
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel B: Excluding Self-Employed Individuals}}} \\ \midrule")
collabels(, none) mlabels(, none) nonumbers label booktabs nonotes
stats(mean rsq N, labels("Control, Pre Mean" "R2" "N")fmt(2 2 0)) 
;
#delimit cr	





*******************************************************
* 2. NSS REGRESSIONS FOR THE MAIN PAPER WITHOUT WEIGHTS
*******************************************************
***Appendix Table A8
est clear 
foreach var of varlist informalworker informalfirm casualworker lndwage_wind {
*	local var "casualworker2"
	 qui eststo reg_`var': reghdfe `var' ${controls} posttreat /// 
			  if indcode>=15 & sample_main == 1 ///
		 , cluster($cl) absorb($fe_main)
	    qui estadd local fes "Yes"
    	qui estadd local controls "No"
    	qui estadd scalar rsq = e(r2)
    	qui summ `var' if treat == 0 & post == 0 & indcode>=15 & sample_main == 1
    	qui estadd scalar mean = r(mean)


	 qui eststo regm_`var': reghdfe `var'_nohh ${controls} posttreat /// 
			  if indcode>=15  & sample_main == 1  & sample_nohh == 1 ///
		 , cluster($cl) absorb($fe_main)
	    qui estadd local fes "Yes"
    	qui estadd local controls "No"
    	qui estadd scalar rsq = e(r2)
    	qui summ `var' if treat == 0 & post == 0 & indcode>=15 & sample_main == 1 & sample_nohh == 1
    	qui estadd scalar mean = r(mean)


    	noi di "Done with `var'"
}


# delimit ;
esttab  reg_* using "${output}/nss/TableA8.tex", replace
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Probability of Working In" " ", pattern(1 0 0 1)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Informal Contract" "Informal Firm" "Casual Work" "Wage")
stats(mean rsq N, labels("Control, 1991 Mean" "R2" "N")
fmt(2 2 0)) collabels(none) label booktabs nonotes
;
#delimit cr	


*******************************************************
* 3. EVENT STUDY INCLUDING ALL YEARS - Table A11
*******************************************************

*EVENT STUDY REGRESSIONS FOR WAGES
local wage = "lndwage_wind"

eststo model_all_wage: reghdfe `wage' treat9394  zero treat0304 treat0405 /// 
		 ${controls} [pw=weight] if indcode>=15 ///
		 , cluster($cl) absorb($fe_m)
    	qui estadd scalar rsq = e(r2)

*EVENT STUDY REGRESSIONS FOR CASUAL WORKERS
local var "casualworker"
eststo model_all_cas: reghdfe `var' treat9394  zero treat0304 treat0405 /// 
		 ${controls} [pw=weight] if indcode>=15 ///
		 , cluster($cl) absorb($fe_m)
    	qui estadd scalar rsq = e(r2)


/*STORING THE EVENT STUDY PLOTS
coefplot (model_all_wage, recast(connected) lcolor(black) mcolor(black) msymbol(square) ciopts(recast(rcap) lcolor(black))) ///
		 (model_all_cas,  recast(connected) lcolor(red) mcolor(red) msymbol(diamond) ciopts(recast(rcap) lcolor(red))) ///
					, vertical yline(0, lcolor(gs10) lp(dash))  ///
					keep(treat* zero) omitted levels (90) ///
					xtitle(" ") bgcolor(white) ///
					graphregion(color(white)) lcolor(emidblue) ///
					ytitle("Coefficient") ///
					legend(pos(6) rows(1) label(2 "Wage") label(4 "Casual Work"))
graph export "${output}/eventstudy_wage_casualwork_nss.png", as(png) name("Graph") replace
*/

# delimit ;
esttab  model_all_wage model_all_cas using "${output}/nss/TableA11.tex", replace
keep(treat9394 zero treat0304 treat0405)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Log Wages" "Casual Workers", pattern(1 1)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles(none)
stats(rsq N, labels("R2" "N")
fmt(2 0)) collabels(none) label booktabs nonotes
;
#delimit cr 







*******************************************************
* 4. MANUFACTURING VS ALL SECTORS -- Table A12
*******************************************************
est clear 
foreach var of varlist informalworker informalfirm casualworker lndwage_wind {
*	local var "casualworker2"
	 qui eststo reg_`var': reghdfe `var' ${controls} posttreat /// 
			 [pw=weight] if indcode>=15 & sample_main == 1 ///
		 , cluster($cl) absorb($fe_main)
	    qui estadd local fes "Yes"
    	qui estadd local controls "No"
    	qui estadd scalar rsq = e(r2)
    	qui summ `var' if treat == 0 & post == 0 & indcode>=15  & sample_main == 1 
    	qui estadd scalar mean = r(mean)


	 qui eststo regm_`var': reghdfe `var' ${controls} posttreat /// 
			 [pw=weight] if indcode>=15 & indcode<=36  & sample_main == 1 ///
		 , cluster($cl) absorb($fe_main)
	    qui estadd local fes "Yes"
    	qui estadd local controls "No"
    	qui estadd scalar rsq = e(r2)
    	qui summ `var' if treat == 0 & post == 0 & indcode>=15 & indcode<=36 & sample_main == 1 
    	qui estadd scalar mean = r(mean)


    	noi di "Done with `var'"
}


foreach var of varlist informalworker informalfirm casualworker lndwage_wind {
*	local var "casualworker2"
	 qui eststo regay_`var': reghdfe `var' ${controls} posttreat /// 
			 [pw=weight] if indcode>=15 ///
		 , cluster($cl) absorb($fe_m)
	    qui estadd local fes "Yes"
    	qui estadd local controls "No"
    	qui estadd scalar rsq = e(r2)
    	qui summ `var' if treat == 0 & post == 0 & indcode>=15
    	qui estadd scalar mean = r(mean)


	 qui eststo regmay_`var': reghdfe `var' ${controls} posttreat /// 
			 [pw=weight] if indcode>=15 & indcode<=36 ///
		 , cluster($cl) absorb($fe_m)
	    qui estadd local fes "Yes"
    	qui estadd local controls "No"
    	qui estadd scalar rsq = e(r2)
    	qui summ `var' if treat == 0 & post == 0 & indcode>=15 & indcode<=36
    	qui estadd scalar mean = r(mean)


    	noi di "Done with `var'"
}


# delimit ;
esttab  regm_* using "${output}/nss/TableA12.tex", replace
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Probability of Working In" " ", pattern(1 0 0 1)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Informal Contract" "Informal Firm" "Casual Work" "Wage")
stats(mean rsq N, labels("Control, Pre Mean" "R2" "N")fmt(2 2 0)) 
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel A: Manufacturing Sector (Main Sample)}}} \\ \midrule")
postfoot("")
collabels(none) label booktabs nonotes
;
#delimit cr	


# delimit ;
esttab  reg_* using "${output}/nss/TableA12.tex", append
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel B: All Sectors (Main Sample)}}} \\ \midrule")
postfoot("")
collabels(, none) mlabels(, none) nonumbers label booktabs nonotes
stats(mean rsq N, labels("Control, Pre Mean" "R2" "N")fmt(2 2 0)) 
;
#delimit cr	

# delimit ;
esttab  regmay_* using "${output}/nss/TableA12.tex", append
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel C: Manufacturing Sector (All Years)}}} \\ \midrule")
postfoot("")
collabels(, none) mlabels(, none) nonumbers label booktabs nonotes
stats(mean rsq N, labels("Control, Pre Mean" "R2" "N")fmt(2 2 0)) 
;
#delimit cr	

# delimit ;
esttab  regay_* using "${output}/nss/TableA12.tex", append
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel D: All Sectors (All Years)}}} \\ \midrule")
collabels(, none) mlabels(, none) nonumbers label booktabs nonotes
stats(mean rsq N, labels("Control, Pre Mean" "R2" "N")fmt(2 2 0)) 
;
#delimit cr	




*******************************************************
* 5. INFORMAL CONTRACT RECLASSIFICATION -- Table A10
*******************************************************
est clear 
foreach var of varlist informalworker informalworker2 {
*	local var "casualworker2"
	 qui eststo reg_`var': reghdfe `var' ${controls} posttreat /// 
			 [pw=weight] if indcode>=15 ///
		 , cluster($cl) absorb($fe_m)
	    qui estadd local fes "Yes"
    	qui estadd local controls "No"
    	qui estadd local samp "All"
    	qui estadd scalar rsq = e(r2)
    	qui summ `var' if treat == 0 & post == 0 
    	qui estadd scalar mean = r(mean)

	 qui eststo regm_`var': reghdfe `var' ${controls} posttreat /// 
			 [pw=weight] if indcode>=15 & sample_main == 1 ///
		 , cluster($cl) absorb($fe_main)
	    qui estadd local fes "Yes"
    	qui estadd local controls "No"
    	qui estadd local samp "Main"
    	qui estadd scalar rsq = e(r2)
    	qui summ `var' if treat == 0 & post == 0 & sample_main == 1
    	qui estadd scalar mean = r(mean)
    	noi di "Done with `var'"
}


# delimit ;
esttab  regm_* reg_* using "${output}/nss/TableA10.tex", replace
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Prob. of Working on Informal Contract", pattern(1  0 0  0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Daily/Weekly" "Daily" "Daily/Weekly" "Daily")
stats(samp mean rsq N, labels("Sample" "\midrule Control, Pre Mean" "R2" "N")
fmt(0 2 2 0)) collabels(none) label booktabs nonotes
;
#delimit cr 








*******************************************************
* 6. OVERLAP ACROSS DIFFERENT CATEGORIES - Table 2
*******************************************************
est clear 
foreach var of varlist informalworker informalfirm casualworker {
	 qui eststo iw_`var': reg `var' [pw=weight] if indcode>=15 & informalworker == 1 & sample_main == 1
	 qui eststo if_`var': reg `var' [pw=weight] if indcode>=15 & informalfirm == 1 & sample_main == 1
	 qui eststo caso_`var': reg `var' [pw=weight] if indcode>=15 & casualworker == 1 & sample_main == 1
    	noi di "Done with `var'"
}


# delimit ;
esttab  iw_* using "${output}/nss/Table2.tex", replace
keep(_cons)
cells(b(fmt(%5.2f)))
mgroups("Informal Contract" "Informal Firm" "Casual Work" "Cas. + OAE", pattern(1 1 1 1)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel A: Informal Contract = 1}}} \\ \midrule")
postfoot("")
collabels(none) label booktabs nonotes noobs
;
#delimit cr	


# delimit ;
esttab  if_* using "${output}/nss/Table2.tex", append
keep(_cons)
cells(b(fmt(%5.2f)))
prehead("")
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel B: Informal Firm = 1}}} \\ \midrule")
postfoot("")
collabels(, none) mlabels(, none) nonumbers label noobs
;
#delimit cr	


# delimit ;
esttab  caso_* using "${output}/nss/Table2.tex", append
keep(_cons)
cells(b(fmt(%5.2f)))
prehead("")
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel C: Casual Workers = 1}}} \\ \midrule")
collabels(, none) mlabels(, none) nonumbers label booktabs nonotes noobs
;
#delimit cr	



